﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinCalcManager;
using Infragistics.Win.CalcEngine;
using WeifenLuo.WinFormsUI.Docking;
namespace BYManageInfoSystem
{
    public partial class Frm_FinInvoiceCheckRec : DockContent
    {
        private string m_strFilter = "";
        private static DGBY.BLL.HxdNoTab m_bllHxdNoTab = new DGBY.BLL.HxdNoTab();
        private int m_nAppendSpaceNum = 0;
        private bool m_bSumStart = false;
        private int m_nSelectRowIndex = 0;
        private DGBY.BLL.SJPackingTab m_bllSJPackingTab = new DGBY.BLL.SJPackingTab();
        private DGBY.BLL.BGCKHTTab m_bllBGCKHTTab = new DGBY.BLL.BGCKHTTab();
        private DGBY.BLL.FactoryDataTab m_bllFactoryDataTab = new DGBY.BLL.FactoryDataTab();
        private Hashtable m_SQLStringList = new Hashtable();
        private ArrayList m_arrayList = new ArrayList();
        private DGBY.BLL.FactoryGoodsTab m_bllFactoryGoodsTab = new DGBY.BLL.FactoryGoodsTab();
        private DGBY.Model.FinCheckInvoiceRecordDetailTab_New m_modelFinCheckInvoiceRecordDetailTab_New = null;

        private static DGBY.BLL.FinCheckInvoiceRecordDetailTab_New m_bllFinCheckInvoiceRecordDetailTab_New = new DGBY.BLL.FinCheckInvoiceRecordDetailTab_New();
        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();
        private DGBY.BLL.FinCheckInvoiceRecordDetailAuxiliaryTab_New m_bllInvoiceAuxiliary_New
            = new DGBY.BLL.FinCheckInvoiceRecordDetailAuxiliaryTab_New();
        private int m_nDelIndex = -1;
        private bool m_bInsertFlag = false;
        private bool m_bCopyInsertFlag = false;
        public static void ultraGrid1_InitializeRowFilter(Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            //ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            //foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
            //    ugc.PerformAutoResize();
            // FILTER ROW FUNCTIONALITY RELATED ULTRAGRID SETTINGS
            //e.Layout.Override.RowFilterAction = RowFilterAction.DisableFilteredOutRows;
            // ----------------------------------------------------------------------------------
            // Enable the the filter row user interface by setting the FilterUIType to FilterRow.
            e.Layout.Override.FilterUIType = FilterUIType.FilterRow;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // FilterEvaluationTrigger specifies when UltraGrid applies the filter criteria typed 
            // into a filter row. Default is OnCellValueChange which will cause the UltraGrid to
            // re-filter the data as soon as the user modifies the value of a filter cell.
            e.Layout.Override.FilterEvaluationTrigger = FilterEvaluationTrigger.OnCellValueChange;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // By default the UltraGrid selects the type of the filter operand editor based on
            // the column's DataType. For DateTime and boolean columns it uses the column's editors.
            // For other column types it uses the Combo. You can explicitly specify the operand
            // editor style by setting the FilterOperandStyle on the override or the individual
            // columns.

            e.Layout.Override.FilterOperandStyle = FilterOperandStyle.DropDownList;

            // By default UltraGrid displays user interface for selecting the filter operator. 
            // You can set the FilterOperatorLocation to hide this user interface. This
            // property is available on column as well so it can be controlled on a per column
            // basis. Default is WithOperand. This property is exposed off the column as well.
            e.Layout.Override.FilterOperatorLocation = FilterOperatorLocation.WithOperand;

            // By default the UltraGrid uses StartsWith as the filter operator. You use
            // the FilterOperatorDefaultValue property to specify a different filter operator
            // to use. This is the default or the initial filter operator value of the cells
            // in filter row. If filter operator user interface is enabled (FilterOperatorLocation
            // is not set to None) then that ui will be initialized to the value of this
            // property. The user can then change the operator as he/she chooses via the operator
            // drop down.
            e.Layout.Override.FilterOperatorDefaultValue = FilterOperatorDefaultValue.StartsWith;

            // FilterOperatorDropDownItems property can be used to control the options provided
            // to the user for selecting the filter operator. By default UltraGrid bases 
            // what operator options to provide on the column's data type. This property is
            // avaibale on the column as well.
            //e.Layout.Override.FilterOperatorDropDownItems = FilterOperatorDropDownItems.All;

            // By default UltraGrid displays a clear button in each cell of the filter row
            // as well as in the row selector of the filter row. When the user clicks this
            // button the associated filter criteria is cleared. You can use the 
            // FilterClearButtonLocation property to control if and where the filter clear
            // buttons are displayed.
            e.Layout.Override.FilterClearButtonLocation = FilterClearButtonLocation.RowAndCell;

            // Appearance of the filter row can be controlled using the FilterRowAppearance proeprty.
            e.Layout.Override.FilterRowAppearance.BackColor = Color.LightYellow;

            // You can use the FilterRowPrompt to display a prompt in the filter row. By default
            // UltraGrid does not display any prompt in the filter row.
            e.Layout.Override.FilterRowPrompt = "Click here to filter data...";

            // You can use the FilterRowPromptAppearance to change the appearance of the prompt.
            // By default the prompt is transparent and uses the same fore color as the filter row.
            // You can make it non-transparent by setting the appearance' BackColorAlpha property 
            // or by setting the BackColor to a desired value.
            e.Layout.Override.FilterRowPromptAppearance.BackColorAlpha = Alpha.Opaque;

            // By default the prompt is spread across multiple cells if it's bigger than the
            // first cell. You can confine the prompt to a particular cell by setting the
            // SpecialRowPromptField property off the band to the key of a column.
            //e.Layout.Bands[0].SpecialRowPromptField = e.Layout.Bands[0].Columns[0].Key;

            // Display a separator between the filter row other rows. SpecialRowSeparator property 
            // can be used to display separators between various 'special' rows, including for the
            // filter row. This property is a flagged enum property so it can take multiple values.
            e.Layout.Override.SpecialRowSeparator = SpecialRowSeparator.FilterRow;

            // You can control the appearance of the separator using the SpecialRowSeparatorAppearance
            // property.
            e.Layout.Override.SpecialRowSeparatorAppearance.BackColor = Color.FromArgb(233, 242, 199);

            //   UltraGridBand band = e.Layout.Bands[0];
            //   band.ColumnFilters["原币金额"].FilterConditions.Clear();
            //   band.ColumnFilters["原币金额"].FilterConditions.Add(

            //FilterComparisionOperator.GreaterThan, 5);
            //   band.ColumnFilters["原币金额"].FilterConditions.Add(

            //FilterComparisionOperator.LessThan, 10);
            //   band.ColumnFilters["原币金额"].LogicalOperator = FilterLogicalOperator.And;
        }
        public Frm_FinInvoiceCheckRec()
        {
            InitializeComponent();
        }
        private void ShowAllRecord(string strWhere)
        {
            DataSet ds = m_bllFinCheckInvoiceRecordDetailTab_New.GetFinInvoiceRecordTabInfo(strWhere);
            ultraGrid1.DataSource = null;

            //DataSet dsTemp = m_bllPaidanRegisterTab.GetAllPaiDanList("");
            //if (dsTemp.Tables.Count > 0 && dsTemp.Tables[0].Rows.Count > 0)
            //{
            //    this.ultraDropDownPaidan.DataSource = dsTemp.Tables[0];
            //    this.ultraDropDownPaidan.ValueMember = "PaidanRegisterTabID";
            //    this.ultraDropDownPaidan.DisplayMember = "合同编号";
            //    for (int i = 0; i < this.ultraDropDownPaidan.DisplayLayout.Bands[0].Columns.Count; i++)
            //        this.ultraDropDownPaidan.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            //    this.ultraDropDownPaidan.DisplayLayout.Bands[0].Columns["合同编号"].Hidden = false;
            //}


            ultraGrid1.DataSource = ds.Tables[0];
            ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            ultraGrid1.DisplayLayout.Bands[0].Columns[1].Hidden = true;
      //      this.ultraGrid1.DisplayLayout.Bands[0].Columns["合同号"].ValueList = this.ultraDropDownPaidan;

        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecord("");
        }

        private Int64 MakeRecvMoneyDetailDJNo()
        {
            string strDate = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString().PadLeft(2, '0') + System.DateTime.Now.Day.ToString().PadLeft(2, '0');
            DataSet ds = m_bllFinCheckInvoiceRecordDetailTab_New.GetSJPackDJNoByDate("DanJuDate = '" + strDate + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                string strDJNo = ds.Tables[0].Rows[0]["MaxJPackDJNum"].ToString();
                long nDJNo = 0;
                if (strDJNo != "")
                    nDJNo = Convert.ToInt64(strDJNo, 10);
                nDJNo++;
                string strTemp = "";
                if (strDJNo != "")
                    strTemp = string.Format("{0:d4}", nDJNo);
                else
                    strTemp = strDate + string.Format("{0:d4}", nDJNo);

                return Convert.ToInt64(strTemp, 10);
            }

            return 0;
        }
        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            if (this.checkBox1.Checked == true)
                ultraGrid1_InitializeRowFilter(e);
            else
                ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
  
            ClassUltraGridCommonSet.LocalPrintPreveiw();
            foreach (UltraGridColumn column2 in ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
                string str = column2.Header.Caption;
                if (str == "部门" || str == "客人" || str == "报关金额" || str == "USD金额" || str == "收票金额"
                    || str == "合同号" || str == "报关开票差额" || str == "申报日期"
                    || str == "经营公司" || str == "编号" || str == "单据日期" || str == "审票日期"
                    || str == "子序号" || str == "是否已退税" || str == "审票年月")
                {
                    column2.CellAppearance.BackColor = Color.Coral;
                    column2.CellActivation = Activation.ActivateOnly;
                }
            }

            InitColumnWidth();
            ClassUltraGridCommonSet.ultraGrid1_RowFilter(this.ultraGrid1, "合同号", m_strFilter);
            m_strFilter = "";
        }

        private void InitColumnWidth()
        {
            ultraGrid1.DisplayLayout.Bands[0].Columns["申报日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["审票日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["审票年月"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["客人"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["合同号"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["部门代码"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["合同号后缀"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["币制"].Width = 40;
            ultraGrid1.DisplayLayout.Bands[0].Columns["报关开票差额"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["报关金额"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["收票金额公式"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["收票金额"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["是否已退税"].Width = 45;
            ultraGrid1.DisplayLayout.Bands[0].Columns["开票USD设17"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["开票HK设16"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["开票HK设15"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["开票HK设13"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["开票HK设9"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["开票HK设5"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["退票原因"].Width = 100;
            ultraGrid1.DisplayLayout.Bands[0].Columns["付税编号"].Width = 80;
            ultraGrid1.DisplayLayout.Bands[0].Columns["备注"].Width = 100;
            ultraGrid1.DisplayLayout.Bands[0].Columns["部门"].Width = 75;
            ultraGrid1.DisplayLayout.Bands[0].Columns["经营公司"].Width = 160;
            ultraGrid1.DisplayLayout.Bands[0].Columns["编号"].Width = 90;
            ultraGrid1.DisplayLayout.Bands[0].Columns["子序号"].Width = 45;
            ultraGrid1.DisplayLayout.Bands[0].Columns["单据日期"].Width = 95;
        }

        private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control)
            {

                //if (!m_bInsertFlag)
                //{
                //    UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
                //    //  row.Cells[2] = DateTime.Parse("1900-01-01");
                //}
            }
            switch (e.KeyCode)
            {

                case Keys.Up:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex < ultraGrid1.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }
        }

        //private void InitNewAddRowValue(int nIndex)
        //{
        //    UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];
        //    CurrentUGR.Cells["单据日期"].Value = DateTime.Now.Date;
        //    CurrentUGR.Cells["合同号"].Value = m_bllPaidanRegisterTab.GetMaxId() - 1;
        //    CurrentUGR.Cells["审票日期"].Value = DateTime.Now.Date;
        //    CurrentUGR.Cells["备注"].Value = "";
        //    CurrentUGR.Cells["退票原因"].Value = "";
        //    CurrentUGR.Cells["子序号"].Value = 0;
        //    CurrentUGR.Cells["是否已退税"].Value = 0;

        //    CurrentUGR.Cells["编号"].Value = MakeRecvMoneyDetailDJNo();
        //    //CurrentUGR.Cells["USD金额"].Value = 0;
        //    //CurrentUGR.Cells["设百分比"].Value = 0.17;
        //    CurrentUGR.Cells["收票金额公式"].Value = "";
        //    CurrentUGR.Cells["付税编号"].Value = "";


        //    CurrentUGR.Cells["开票USD设17"].Value = 0;
        //    CurrentUGR.Cells["开票USD设16"].Value = 0;
        //    CurrentUGR.Cells["开票USD设15"].Value = 0;
        //    CurrentUGR.Cells["开票USD设13"].Value = 0;
        //    CurrentUGR.Cells["开票USD设9"].Value = 0;
        //    CurrentUGR.Cells["开票USD设5"].Value = 0;

        //    CurrentUGR.Cells["开票HK设17"].Value = 0;
        //    CurrentUGR.Cells["开票HK设16"].Value = 0;
        //    CurrentUGR.Cells["开票HK设15"].Value = 0;
        //    CurrentUGR.Cells["开票HK设13"].Value = 0;
        //    CurrentUGR.Cells["开票HK设9"].Value = 0;
        //    CurrentUGR.Cells["开票HK设5"].Value = 0;

        //    CurrentUGR.Cells["美元汇率"].Value = 0;
        //    CurrentUGR.Cells["港币汇率"].Value = 0;

        //}
        private void ultraGrid1_AfterRowInsert(object sender, RowEventArgs e)
        {
            ultraGrid1.Rows[e.Row.Index].Cells[1].IgnoreRowColActivation = true;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activation = Activation.AllowEdit;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activate();
            ultraGrid1.PerformAction(UltraGridAction.EnterEditMode);
            m_bInsertFlag = true;
            //if (m_bCopyInsertFlag == false)
            //    InitNewAddRowValue(e.Row.Index);
            //else
            {
                m_bCopyInsertFlag = false;
                InitNewAddRowValueCopyContract(e.Row.Index);
            }
            
        }

        private void InitNewAddRowValueCopyContract(int nIndex)
        {
            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];
            CurrentUGR.Cells["单据日期"].Value = DateTime.Now.Date;
            CurrentUGR.Cells["合同号"].Value = m_modelFinCheckInvoiceRecordDetailTab_New.PaidanRegisterTabID;
            CurrentUGR.Cells["审票日期"].Value = DateTime.Now.Date;
            CurrentUGR.Cells["备注"].Value = "";
            CurrentUGR.Cells["退票原因"].Value = "";
            CurrentUGR.Cells["子序号"].Value = 0;
            CurrentUGR.Cells["是否已退税"].Value = 0;
            CurrentUGR.Cells["编号"].Value = "";
            //CurrentUGR.Cells["编号"].Value = MakeRecvMoneyDetailDJNo();
            //CurrentUGR.Cells["USD金额"].Value = 0;
            //CurrentUGR.Cells["设百分比"].Value = 0.17;
            CurrentUGR.Cells["收票金额公式"].Value = "";
            CurrentUGR.Cells["付税编号"].Value = "";


            CurrentUGR.Cells["开票USD设17"].Value = 0;
            CurrentUGR.Cells["开票USD设16"].Value = 0;
            CurrentUGR.Cells["开票USD设15"].Value = 0;
            CurrentUGR.Cells["开票USD设13"].Value = 0;
            CurrentUGR.Cells["开票USD设9"].Value = 0;
            CurrentUGR.Cells["开票USD设5"].Value = 0;

            CurrentUGR.Cells["开票HK设17"].Value = 0;
            CurrentUGR.Cells["开票HK设16"].Value = 0;
            CurrentUGR.Cells["开票HK设15"].Value = 0;
            CurrentUGR.Cells["开票HK设13"].Value = 0;
            CurrentUGR.Cells["开票HK设9"].Value = 0;
            CurrentUGR.Cells["开票HK设5"].Value = 0;


            CurrentUGR.Cells["美元汇率"].Value = m_modelFinCheckInvoiceRecordDetailTab_New.USDHuiLv;
            CurrentUGR.Cells["港币汇率"].Value = m_modelFinCheckInvoiceRecordDetailTab_New.HKHuiLv;



          //  UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

            //CurrentUGR.Cells["合同号"].Value = m_modelFinCheckInvoiceRecordDetailTab_New.PaidanRegisterTabID;
            //CurrentUGR.Cells["开票日期"].Value = m_modelFinCheckInvoiceRecordDetailTab_New.KaiPiaoDate;
            //CurrentUGR.Cells["录单日期"].Value = DateTime.Now.Date;
            //CurrentUGR.Cells["发票号码"].Value = "";
            ////CurrentUGR.Cells["系数A公式"].Value = "";
            //CurrentUGR.Cells["金额"].Value = 0;
            //CurrentUGR.Cells["税金"].Value = 0;
            //CurrentUGR.Cells["开票汇率"].Value = m_modelFinCheckInvoiceRecordDetailTab_New.KaiPiaoHuiLv;
            //CurrentUGR.Cells["港币开票汇率"].Value = m_modelFinCheckInvoiceRecordDetailTab_New.HKKaiPiaoHuiLv;
            //CurrentUGR.Cells["退税点"].Value = 1.02;
            //CurrentUGR.Cells["系数A"].Value = 0.095;
            //CurrentUGR.Cells["系数B"].Value = 1.17;
            //CurrentUGR.Cells["价税合计公式选择"].Value = 0;
            //CurrentUGR.Cells["金额核对忽略"].Value = 0;
            //CurrentUGR.Cells["是否已做完购货合同"].Value = "否";
            if (m_bInsertFlag)
            {
                ultraGrid1.Rows[nIndex].Cells[2].Activation = Activation.ActivateOnly;
                m_bInsertFlag = false;
                AddGoodsDetail(nIndex);

            }

        }
        
        private string GetWhereSets()
        {
            Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

            string strWhere = "";

            for (int i = 0; i < ugr.Length; i++)
            {

                //int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                if (ugr[i].Cells[0].Value.ToString() == "")
                    continue;
                strWhere += " or FinCheckInvoiceRecordDetailTab_NewID = '" + ugr[i].Cells[0].Value.ToString() + "'";

            }
            return strWhere;
        }
        private bool AddGoodsDetail(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                DGBY.Model.FinCheckInvoiceRecordDetailTab_New modelFinCheckInvoiceRecordDetailTab_New = new DGBY.Model.FinCheckInvoiceRecordDetailTab_New();

                modelFinCheckInvoiceRecordDetailTab_New.PayTaxBianHao = CurrentUGR.Cells["付税编号"].Value.ToString();
                modelFinCheckInvoiceRecordDetailTab_New.PaidanRegisterTabID = Convert.ToInt32(CurrentUGR.Cells["合同号"].Value.ToString(), 10);
                DateTime dtTemp = (DateTime)CurrentUGR.Cells["审票日期"].Value;
                modelFinCheckInvoiceRecordDetailTab_New.ShenPiaoDate = dtTemp.Date;

                modelFinCheckInvoiceRecordDetailTab_New.Note = CurrentUGR.Cells["备注"].Value.ToString();
                modelFinCheckInvoiceRecordDetailTab_New.TuiPiaoReason = CurrentUGR.Cells["退票原因"].Value.ToString();

                modelFinCheckInvoiceRecordDetailTab_New.USDPercent17 = Convert.ToDecimal(CurrentUGR.Cells["开票USD设17"].Value.ToString());
                modelFinCheckInvoiceRecordDetailTab_New.USDPercent16 = Convert.ToDecimal(CurrentUGR.Cells["开票USD设16"].Value.ToString());
                modelFinCheckInvoiceRecordDetailTab_New.USDPercent15 = Convert.ToDecimal(CurrentUGR.Cells["开票USD设15"].Value.ToString());
                modelFinCheckInvoiceRecordDetailTab_New.USDPercent13 = Convert.ToDecimal(CurrentUGR.Cells["开票USD设13"].Value.ToString());
                modelFinCheckInvoiceRecordDetailTab_New.USDPercent9 = Convert.ToDecimal(CurrentUGR.Cells["开票USD设9"].Value.ToString());
                modelFinCheckInvoiceRecordDetailTab_New.USDPercent5 = Convert.ToDecimal(CurrentUGR.Cells["开票USD设5"].Value.ToString());

                modelFinCheckInvoiceRecordDetailTab_New.HKPercent17 = Convert.ToDecimal(CurrentUGR.Cells["开票HK设17"].Value.ToString());
                modelFinCheckInvoiceRecordDetailTab_New.HKPercent16 = Convert.ToDecimal(CurrentUGR.Cells["开票HK设16"].Value.ToString());
                modelFinCheckInvoiceRecordDetailTab_New.HKPercent15 = Convert.ToDecimal(CurrentUGR.Cells["开票HK设15"].Value.ToString());
                modelFinCheckInvoiceRecordDetailTab_New.HKPercent13 = Convert.ToDecimal(CurrentUGR.Cells["开票HK设13"].Value.ToString());
                modelFinCheckInvoiceRecordDetailTab_New.HKPercent9 = Convert.ToDecimal(CurrentUGR.Cells["开票HK设9"].Value.ToString());
                modelFinCheckInvoiceRecordDetailTab_New.HKPercent5 = Convert.ToDecimal(CurrentUGR.Cells["开票HK设5"].Value.ToString());

                modelFinCheckInvoiceRecordDetailTab_New.USDHuiLv = Convert.ToDecimal(CurrentUGR.Cells["美元汇率"].Value.ToString());
                modelFinCheckInvoiceRecordDetailTab_New.HKHuiLv = Convert.ToDecimal(CurrentUGR.Cells["港币汇率"].Value.ToString());
                

                //modelFinCheckInvoiceRecordDetailTab_New.USDJinE = Convert.ToDecimal(CurrentUGR.Cells["USD金额"].Value.ToString());
                //modelFinCheckInvoiceRecordDetailTab_New.SetPercent = Convert.ToDecimal(CurrentUGR.Cells["设百分比"].Value.ToString());
                modelFinCheckInvoiceRecordDetailTab_New.TransGongShi = CurrentUGR.Cells["收票金额公式"].Value.ToString();

                dtTemp = (DateTime)CurrentUGR.Cells["单据日期"].Value;
                modelFinCheckInvoiceRecordDetailTab_New.DanJuDate = dtTemp.Date;
                modelFinCheckInvoiceRecordDetailTab_New.BianHao = CurrentUGR.Cells["编号"].Value.ToString();
                modelFinCheckInvoiceRecordDetailTab_New.SubSeqNo = Convert.ToInt32(CurrentUGR.Cells["子序号"].Value.ToString(), 10);
                modelFinCheckInvoiceRecordDetailTab_New.IsPayTaxDone = Convert.ToInt32(CurrentUGR.Cells["是否已退税"].Value.ToString(), 10);

                int nAddId = m_bllFinCheckInvoiceRecordDetailTab_New.Add(modelFinCheckInvoiceRecordDetailTab_New);

                Frm_Main.Log2DB(this.Text, "增加", CurrentUGR.Cells["合同号"].Text);

               
                ShowAllRecord("");
                //string strWhere = GetWhereSets();
                //ShowAllRecord("FinCheckInvoiceRecordDetailTab_NewID = '" + nAddId.ToString() + "'" + strWhere);

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private Dictionary<Decimal, Decimal> ComputeUSD2(DGBY.Model.FinCheckInvoiceRecordDetailTab_New modelFinCheckInvoiceRecordDetailTab_New)
        {
            Dictionary<Decimal, Decimal> dicPrecent2JinE = new Dictionary<decimal, decimal>();

            if (modelFinCheckInvoiceRecordDetailTab_New.USDPercent17 > 0)
                dicPrecent2JinE.Add(0.17M, modelFinCheckInvoiceRecordDetailTab_New.USDPercent17);
            if (modelFinCheckInvoiceRecordDetailTab_New.USDPercent16 > 0)
                dicPrecent2JinE.Add(0.16M, modelFinCheckInvoiceRecordDetailTab_New.USDPercent16);

            if (modelFinCheckInvoiceRecordDetailTab_New.USDPercent15 > 0)
                dicPrecent2JinE.Add(0.15M, modelFinCheckInvoiceRecordDetailTab_New.USDPercent15);
            if (modelFinCheckInvoiceRecordDetailTab_New.USDPercent13 > 0)
                dicPrecent2JinE.Add(0.13M, modelFinCheckInvoiceRecordDetailTab_New.USDPercent13);
            if (modelFinCheckInvoiceRecordDetailTab_New.USDPercent9 > 0)
                dicPrecent2JinE.Add(0.09M, modelFinCheckInvoiceRecordDetailTab_New.USDPercent9);
            if (modelFinCheckInvoiceRecordDetailTab_New.USDPercent5 > 0)
                dicPrecent2JinE.Add(0.05M, modelFinCheckInvoiceRecordDetailTab_New.USDPercent5);

            return dicPrecent2JinE;
        }
        private Dictionary<Decimal, Decimal> ComputeHKD(DGBY.Model.FinCheckInvoiceRecordDetailTab_New modelFinCheckInvoiceRecordDetailTab_New
     , DGBY.Model.FinCheckInvoiceRecordDetailTab_New modelFinCheckInvoiceRecordDetailTab_Old)
        {
            Dictionary<Decimal, Decimal> dicPrecent2JinE = new Dictionary<decimal, decimal>();

            if (modelFinCheckInvoiceRecordDetailTab_New.HKPercent17 != modelFinCheckInvoiceRecordDetailTab_Old.HKPercent17)
                dicPrecent2JinE.Add(0.17M, modelFinCheckInvoiceRecordDetailTab_New.HKPercent17);
            if (modelFinCheckInvoiceRecordDetailTab_New.HKPercent16 != modelFinCheckInvoiceRecordDetailTab_Old.HKPercent16)
                dicPrecent2JinE.Add(0.16M, modelFinCheckInvoiceRecordDetailTab_New.HKPercent16);

            if (modelFinCheckInvoiceRecordDetailTab_New.HKPercent15 != modelFinCheckInvoiceRecordDetailTab_Old.HKPercent15)
                dicPrecent2JinE.Add(0.15M, modelFinCheckInvoiceRecordDetailTab_New.HKPercent15);
            if (modelFinCheckInvoiceRecordDetailTab_New.HKPercent13 != modelFinCheckInvoiceRecordDetailTab_Old.HKPercent13)
                dicPrecent2JinE.Add(0.13M, modelFinCheckInvoiceRecordDetailTab_New.HKPercent13);
            if (modelFinCheckInvoiceRecordDetailTab_New.HKPercent9 != modelFinCheckInvoiceRecordDetailTab_Old.HKPercent9)
                dicPrecent2JinE.Add(0.09M, modelFinCheckInvoiceRecordDetailTab_New.HKPercent9);
            if (modelFinCheckInvoiceRecordDetailTab_New.HKPercent5 != modelFinCheckInvoiceRecordDetailTab_Old.HKPercent5)
                dicPrecent2JinE.Add(0.05M, modelFinCheckInvoiceRecordDetailTab_New.HKPercent5);

            return dicPrecent2JinE;
        }

        private Dictionary<Decimal, Decimal> ComputeUSD(DGBY.Model.FinCheckInvoiceRecordDetailTab_New modelFinCheckInvoiceRecordDetailTab_New
            , DGBY.Model.FinCheckInvoiceRecordDetailTab_New modelFinCheckInvoiceRecordDetailTab_Old)
        {
            Dictionary<Decimal, Decimal> dicPrecent2JinE = new Dictionary<decimal, decimal>();

            if (modelFinCheckInvoiceRecordDetailTab_New.USDPercent17 != modelFinCheckInvoiceRecordDetailTab_Old.USDPercent17)
                dicPrecent2JinE.Add(0.17M, modelFinCheckInvoiceRecordDetailTab_New.USDPercent17);
            if (modelFinCheckInvoiceRecordDetailTab_New.USDPercent16 != modelFinCheckInvoiceRecordDetailTab_Old.USDPercent16)
                dicPrecent2JinE.Add(0.16M, modelFinCheckInvoiceRecordDetailTab_New.USDPercent16);

            if (modelFinCheckInvoiceRecordDetailTab_New.USDPercent15 != modelFinCheckInvoiceRecordDetailTab_Old.USDPercent15)
                dicPrecent2JinE.Add(0.15M, modelFinCheckInvoiceRecordDetailTab_New.USDPercent15);
            if (modelFinCheckInvoiceRecordDetailTab_New.USDPercent13 != modelFinCheckInvoiceRecordDetailTab_Old.USDPercent13)
                dicPrecent2JinE.Add(0.13M, modelFinCheckInvoiceRecordDetailTab_New.USDPercent13);
            if (modelFinCheckInvoiceRecordDetailTab_New.USDPercent9 != modelFinCheckInvoiceRecordDetailTab_Old.USDPercent9)
                dicPrecent2JinE.Add(0.09M, modelFinCheckInvoiceRecordDetailTab_New.USDPercent9);
            if (modelFinCheckInvoiceRecordDetailTab_New.USDPercent5 != modelFinCheckInvoiceRecordDetailTab_Old.USDPercent5)
                dicPrecent2JinE.Add(0.05M, modelFinCheckInvoiceRecordDetailTab_New.USDPercent5);

            return dicPrecent2JinE;
        }

        //private Decimal ComputeUSD(DGBY.Model.FinCheckInvoiceRecordDetailTab_New modelFinCheckInvoiceRecordDetailTab_New)
        //{
        //    Decimal dtTemp = 0;
        //    dtTemp = modelFinCheckInvoiceRecordDetailTab_New.USDPercent17
        //            + modelFinCheckInvoiceRecordDetailTab_New.USDPercent16
        //            + modelFinCheckInvoiceRecordDetailTab_New.USDPercent15
        //            + modelFinCheckInvoiceRecordDetailTab_New.USDPercent13
        //            + modelFinCheckInvoiceRecordDetailTab_New.USDPercent9
        //            + modelFinCheckInvoiceRecordDetailTab_New.USDPercent5;
        //    if (dtTemp > 0)
        //        return dtTemp;
        //    else if (modelFinCheckInvoiceRecordDetailTab_New.USDHuiLv > 0)
        //    {
        //        dtTemp = (modelFinCheckInvoiceRecordDetailTab_New.HKPercent17
        //            + modelFinCheckInvoiceRecordDetailTab_New.HKPercent16
        //            + modelFinCheckInvoiceRecordDetailTab_New.HKPercent15
        //            + modelFinCheckInvoiceRecordDetailTab_New.HKPercent13
        //            + modelFinCheckInvoiceRecordDetailTab_New.HKPercent9
        //            + modelFinCheckInvoiceRecordDetailTab_New.HKPercent5) * modelFinCheckInvoiceRecordDetailTab_New.HKHuiLv / modelFinCheckInvoiceRecordDetailTab_New.USDHuiLv;
        //        return dtTemp;
        //    }
        //    return dtTemp;
        //}
        //现在是一个退税点一条审票补充记录
        //如果是退了税之后来改动审票补充记录这里呢？
        private void ProcessFinCheckInvoiceRecordDetailAuxiliaryTab_New(Dictionary < Decimal, Decimal > dicPrecent2JinE
            , DGBY.Model.FinCheckInvoiceRecordDetailTab_New modelFinCheckInvoiceRecordDetailTab_New)
        {

            foreach (Decimal dtTemp in dicPrecent2JinE.Keys)
            {
                m_nAppendSpaceNum++;
                DataSet ds = m_bllInvoiceAuxiliary_New.GetList("FinCheckInvoiceRecordDetailTab_NewID = '"
                         + modelFinCheckInvoiceRecordDetailTab_New.FinCheckInvoiceRecordDetailTab_NewID.ToString()
                         + "' and PayTaxPercent = '" + dtTemp.ToString() + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    int nID = Convert.ToInt32(ds.Tables[0].Rows[0]["FinCheckInvoiceRecordDetailAuxiliaryTab_NewID"].ToString(), 10);
                    DGBY.Model.FinCheckInvoiceRecordDetailAuxiliaryTab_New modelFinCheckInvoiceRecordDetailAuxiliaryTab_New
                        = m_bllInvoiceAuxiliary_New.GetModel(nID);
                    modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayTaxDate = System.DateTime.Now.Date;
                    //modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayedTaxMoneyValue = 0;
                    modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayTaxMoneyValue = dicPrecent2JinE[dtTemp];
                    if(modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayTaxMoneyValue == 0)
                        m_bllInvoiceAuxiliary_New.TransDelete(nID, m_SQLStringList, m_arrayList, m_nAppendSpaceNum);
                    else
                        m_bllInvoiceAuxiliary_New.TransUpdate(modelFinCheckInvoiceRecordDetailAuxiliaryTab_New
                         , m_SQLStringList, m_arrayList, m_nAppendSpaceNum);
                }
                else
                {
                    DGBY.Model.FinCheckInvoiceRecordDetailAuxiliaryTab_New modelFinCheckInvoiceRecordDetailAuxiliaryTab_New
    = new DGBY.Model.FinCheckInvoiceRecordDetailAuxiliaryTab_New();
                    modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.FinCheckInvoiceRecordDetailTab_NewID
                        = modelFinCheckInvoiceRecordDetailTab_New.FinCheckInvoiceRecordDetailTab_NewID;
                    modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.IsPayTaxDone = 0;
                    modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.Note = "";
                    modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayTaxDate = System.DateTime.Now.Date;
                    modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.SubSeqNo = 0;
                    modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayTaxMoneyValue = dicPrecent2JinE[dtTemp];
                    modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayTaxPercent = dtTemp;
                    modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayedTaxMoneyValue = 0;

                    m_bllInvoiceAuxiliary_New.TransAdd(modelFinCheckInvoiceRecordDetailAuxiliaryTab_New
                        , m_SQLStringList, m_arrayList, m_nAppendSpaceNum);
                }
            }

     //       if (dtOldUSDJinE == 0
     //               && dtSum > 0
     //               && modelFinCheckInvoiceRecordDetailTab_New.IsPayTaxDone == 0)//第一次修改原币金额
     //           {
     //               DGBY.Model.FinCheckInvoiceRecordDetailAuxiliaryTab_New modelFinCheckInvoiceRecordDetailAuxiliaryTab_New
     //                   = new DGBY.Model.FinCheckInvoiceRecordDetailAuxiliaryTab_New();
     //               modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.FinCheckInvoiceRecordDetailTab_NewID 
     //                   = modelFinCheckInvoiceRecordDetailTab_New.FinCheckInvoiceRecordDetailTab_NewID;
     //               modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.IsPayTaxDone = 0;
     //               modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.Note = "";
     //               modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayTaxDate = System.DateTime.Now.Date;
     //               modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.SubSeqNo = 0;
     //               modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayTaxMoneyValue = dtSum;
     //               modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayedTaxMoneyValue = 0;

     //               m_bllInvoiceAuxiliary_New.TransAdd(modelFinCheckInvoiceRecordDetailAuxiliaryTab_New
     //                   ,m_SQLStringList, m_arrayList, nAppendSpaceNum);
     //           }
     //            if (dtOldUSDJinE > 0
     //&& dtSum > 0
     //&& modelFinCheckInvoiceRecordDetailTab_New.IsPayTaxDone == 0)//非第一次修改原币金额
     //            {
     //                DataSet ds = m_bllInvoiceAuxiliary_New.GetList("FinCheckInvoiceRecordDetailTab_NewID = '"
     //                    + modelFinCheckInvoiceRecordDetailTab_New.FinCheckInvoiceRecordDetailTab_NewID.ToString() + "'");
     //                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
     //                {
     //                    int nID = Convert.ToInt32(ds.Tables[0].Rows[0]["FinCheckInvoiceRecordDetailAuxiliaryTab_NewID"].ToString(), 10);
     //                    DGBY.Model.FinCheckInvoiceRecordDetailAuxiliaryTab_New modelFinCheckInvoiceRecordDetailAuxiliaryTab_New
     //                        = m_bllInvoiceAuxiliary_New.GetModel(nID);
     //                    modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayTaxDate = System.DateTime.Now.Date;
     //                    modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayedTaxMoneyValue = 0;
     //                    modelFinCheckInvoiceRecordDetailAuxiliaryTab_New.PayTaxMoneyValue = dtSum;
     //                    m_bllInvoiceAuxiliary_New.TransUpdate(modelFinCheckInvoiceRecordDetailAuxiliaryTab_New
     //                         , m_SQLStringList, m_arrayList, nAppendSpaceNum);
     //                }
     //            }
        }

        private bool ModifyGoodsDetail(int nRowIndex)
        {

            try
            {
                m_SQLStringList.Clear();
                m_arrayList.Clear();
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];
                int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                DGBY.Model.FinCheckInvoiceRecordDetailTab_New modelFinCheckInvoiceRecordDetailTab_New = m_bllFinCheckInvoiceRecordDetailTab_New.GetModel(nSelectFinRecvMoneyDetailTab_NewID);
                DGBY.Model.FinCheckInvoiceRecordDetailTab_New modelFinCheckInvoiceRecordDetailTab_Old = m_bllFinCheckInvoiceRecordDetailTab_New.GetModel(nSelectFinRecvMoneyDetailTab_NewID); ;
                string strDanWei = CurrentUGR.Cells["合同号"].Value.ToString();
                string strBiZhi = CurrentUGR.Cells["币制"].Value.ToString();
                if (modelFinCheckInvoiceRecordDetailTab_New.IsPayTaxDone > 0)
                {
                    MessageBox.Show("已退税，不能修改记录？", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return true;

                }
            //    Decimal dtOldUSDJinE = modelFinCheckInvoiceRecordDetailTab_New.USDJinE;
                //Dictionary<Decimal, Decimal> dicOldPrecent2JinE = ComputeUSD(modelFinCheckInvoiceRecordDetailTab_New);
                //string strDanWei = CurrentUGR.Cells["单位"].Value.ToString();
                if (ClassInputValid.IsNum(strDanWei))
                {
                    modelFinCheckInvoiceRecordDetailTab_New.PaidanRegisterTabID = Convert.ToInt32(CurrentUGR.Cells["合同号"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllPaidanRegisterTab.GetList("PaidanRegisterTab.ContractCode = '" + strDanWei + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelFinCheckInvoiceRecordDetailTab_New.PaidanRegisterTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["PaidanRegisterTabID"].ToString(), 10);
                    }
                }
                //DateTime dtTemp = (DateTime)CurrentUGR.Cells["审票日期"].Value;
                modelFinCheckInvoiceRecordDetailTab_New.ShenPiaoDate = System.DateTime.Now.Date;

                modelFinCheckInvoiceRecordDetailTab_New.PayTaxBianHao = CurrentUGR.Cells["付税编号"].Value.ToString();
                modelFinCheckInvoiceRecordDetailTab_New.Note = CurrentUGR.Cells["备注"].Value.ToString();
                modelFinCheckInvoiceRecordDetailTab_New.TuiPiaoReason = CurrentUGR.Cells["退票原因"].Value.ToString();

                //modelFinCheckInvoiceRecordDetailTab_New.USDJinE = Convert.ToDecimal(CurrentUGR.Cells["USD金额"].Value.ToString());
                //modelFinCheckInvoiceRecordDetailTab_New.SetPercent = Convert.ToDecimal(CurrentUGR.Cells["设百分比"].Value.ToString());

                //Convert.ToDecimal(CurrentUGR.Cells["收票金额"].Value.ToString());

                modelFinCheckInvoiceRecordDetailTab_New.HKPercent17 = Convert.ToDecimal(CurrentUGR.Cells["开票HK设17"].Value.ToString());
                modelFinCheckInvoiceRecordDetailTab_New.HKPercent16 = Convert.ToDecimal(CurrentUGR.Cells["开票HK设16"].Value.ToString());
                modelFinCheckInvoiceRecordDetailTab_New.HKPercent15 = Convert.ToDecimal(CurrentUGR.Cells["开票HK设15"].Value.ToString());
                modelFinCheckInvoiceRecordDetailTab_New.HKPercent13 = Convert.ToDecimal(CurrentUGR.Cells["开票HK设13"].Value.ToString());
                modelFinCheckInvoiceRecordDetailTab_New.HKPercent9 = Convert.ToDecimal(CurrentUGR.Cells["开票HK设9"].Value.ToString());
                modelFinCheckInvoiceRecordDetailTab_New.HKPercent5 = Convert.ToDecimal(CurrentUGR.Cells["开票HK设5"].Value.ToString());

                int nNotEqualZeroHkCount = 0;
                if (modelFinCheckInvoiceRecordDetailTab_New.HKPercent17 != 0)
                    nNotEqualZeroHkCount++;
                if (modelFinCheckInvoiceRecordDetailTab_New.HKPercent16 != 0)
                    nNotEqualZeroHkCount++;
                if (modelFinCheckInvoiceRecordDetailTab_New.HKPercent15 != 0)
                    nNotEqualZeroHkCount++;
                if (modelFinCheckInvoiceRecordDetailTab_New.HKPercent13 != 0)
                    nNotEqualZeroHkCount++;
                if (modelFinCheckInvoiceRecordDetailTab_New.HKPercent9 != 0)
                    nNotEqualZeroHkCount++;
                if (modelFinCheckInvoiceRecordDetailTab_New.HKPercent5 != 0)
                    nNotEqualZeroHkCount++;

                if (nNotEqualZeroHkCount > 1)
                {
                    MessageBox.Show("每行记录只能设置一个退税点开票金额", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return true;
                }

                modelFinCheckInvoiceRecordDetailTab_New.USDHuiLv = Convert.ToDecimal(CurrentUGR.Cells["美元汇率"].Value.ToString());
                modelFinCheckInvoiceRecordDetailTab_New.HKHuiLv = Convert.ToDecimal(CurrentUGR.Cells["港币汇率"].Value.ToString());
                if (modelFinCheckInvoiceRecordDetailTab_New.USDHuiLv > 0 &&
                    modelFinCheckInvoiceRecordDetailTab_New.HKHuiLv > 0)
                {
                    Decimal dtTempHuilv = modelFinCheckInvoiceRecordDetailTab_New.HKHuiLv / modelFinCheckInvoiceRecordDetailTab_New.USDHuiLv;
                    modelFinCheckInvoiceRecordDetailTab_New.USDPercent17 = modelFinCheckInvoiceRecordDetailTab_New.HKPercent17 * dtTempHuilv;
                    modelFinCheckInvoiceRecordDetailTab_New.USDPercent16 = modelFinCheckInvoiceRecordDetailTab_New.HKPercent16 * dtTempHuilv;
                    modelFinCheckInvoiceRecordDetailTab_New.USDPercent15 = modelFinCheckInvoiceRecordDetailTab_New.HKPercent15 * dtTempHuilv;
                    modelFinCheckInvoiceRecordDetailTab_New.USDPercent13 = modelFinCheckInvoiceRecordDetailTab_New.HKPercent13 * dtTempHuilv;
                    modelFinCheckInvoiceRecordDetailTab_New.USDPercent9 = modelFinCheckInvoiceRecordDetailTab_New.HKPercent9 * dtTempHuilv;
                    modelFinCheckInvoiceRecordDetailTab_New.USDPercent5 = modelFinCheckInvoiceRecordDetailTab_New.HKPercent5 * dtTempHuilv;

                    CurrentUGR.Cells["开票USD设17"].Value = modelFinCheckInvoiceRecordDetailTab_New.USDPercent17;
                    CurrentUGR.Cells["开票USD设16"].Value = modelFinCheckInvoiceRecordDetailTab_New.USDPercent16;
                    CurrentUGR.Cells["开票USD设15"].Value = modelFinCheckInvoiceRecordDetailTab_New.USDPercent15;
                    CurrentUGR.Cells["开票USD设13"].Value = modelFinCheckInvoiceRecordDetailTab_New.USDPercent13;
                    CurrentUGR.Cells["开票USD设9"].Value = modelFinCheckInvoiceRecordDetailTab_New.USDPercent9;
                    CurrentUGR.Cells["开票USD设5"].Value = modelFinCheckInvoiceRecordDetailTab_New.USDPercent5;
                
                }
                else
                {
                    modelFinCheckInvoiceRecordDetailTab_New.USDPercent17 = Convert.ToDecimal(CurrentUGR.Cells["开票USD设17"].Value.ToString());
                    modelFinCheckInvoiceRecordDetailTab_New.USDPercent16 = Convert.ToDecimal(CurrentUGR.Cells["开票USD设16"].Value.ToString());
                    modelFinCheckInvoiceRecordDetailTab_New.USDPercent15 = Convert.ToDecimal(CurrentUGR.Cells["开票USD设15"].Value.ToString());
                    modelFinCheckInvoiceRecordDetailTab_New.USDPercent13 = Convert.ToDecimal(CurrentUGR.Cells["开票USD设13"].Value.ToString());
                    modelFinCheckInvoiceRecordDetailTab_New.USDPercent9 = Convert.ToDecimal(CurrentUGR.Cells["开票USD设9"].Value.ToString());
                    modelFinCheckInvoiceRecordDetailTab_New.USDPercent5 = Convert.ToDecimal(CurrentUGR.Cells["开票USD设5"].Value.ToString());
                }

                int nNotEqualZeroUsdCount = 0;
                if (modelFinCheckInvoiceRecordDetailTab_New.USDPercent17 != 0)
                    nNotEqualZeroUsdCount++;
                if (modelFinCheckInvoiceRecordDetailTab_New.USDPercent16 != 0)
                    nNotEqualZeroUsdCount++;
                if (modelFinCheckInvoiceRecordDetailTab_New.USDPercent15 != 0)
                    nNotEqualZeroUsdCount++;
                if (modelFinCheckInvoiceRecordDetailTab_New.USDPercent13 != 0)
                    nNotEqualZeroUsdCount++;
                if (modelFinCheckInvoiceRecordDetailTab_New.USDPercent9 != 0)
                    nNotEqualZeroUsdCount++;
                if (modelFinCheckInvoiceRecordDetailTab_New.USDPercent5 != 0)
                    nNotEqualZeroUsdCount++;

                if (nNotEqualZeroUsdCount > 1)
                {
                    MessageBox.Show("每行记录只能设置一个退税点开票金额", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return true;
                }

                DataSet dsFac = m_bllFinCheckInvoiceRecordDetailTab_New.GetFacInfoList("FinCheckInvoiceRecordDetailTab_Newid = '"+ modelFinCheckInvoiceRecordDetailTab_New.FinCheckInvoiceRecordDetailTab_NewID + "'");
                bool bIsHkFac = (bool)dsFac.Tables[0].Rows[0]["是否港币出货工厂"];
                Dictionary<Decimal, Decimal> dicNewPrecent2JinE;
                if (bIsHkFac)
                {
                    dicNewPrecent2JinE
                        = ComputeHKD(modelFinCheckInvoiceRecordDetailTab_New, modelFinCheckInvoiceRecordDetailTab_Old);
                }
                else
                {
                    dicNewPrecent2JinE
                        = ComputeUSD(modelFinCheckInvoiceRecordDetailTab_New, modelFinCheckInvoiceRecordDetailTab_Old);
                }

                modelFinCheckInvoiceRecordDetailTab_New.TransGongShi = CurrentUGR.Cells["收票金额公式"].Value.ToString();
                m_nAppendSpaceNum = 0;
                ProcessFinCheckInvoiceRecordDetailAuxiliaryTab_New(dicNewPrecent2JinE, modelFinCheckInvoiceRecordDetailTab_New);
                  
                m_bllFinCheckInvoiceRecordDetailTab_New.TransUpdate(modelFinCheckInvoiceRecordDetailTab_New
                    , m_SQLStringList, m_arrayList, 0);
              //  ShowAllRecord("");
                // ShowAllRecord("BianHao = '" + modelFinRecvMoneyDetailTab_New.BianHao + "'");
                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                CurrentUGR.Cells["审票日期"].Value = System.DateTime.Now.Date;
                CurrentUGR.Cells["审票年月"].Value = System.DateTime.Now.Date.Year.ToString() + System.DateTime.Now.Date.Month.ToString().PadLeft(2,'0');
                if (strBiZhi == "美元")
                {
                    CurrentUGR.Cells["报关开票差额"].Value = Convert.ToDecimal(CurrentUGR.Cells["报关金额"].Value.ToString())
                        - modelFinCheckInvoiceRecordDetailTab_New.USDPercent17
                        - modelFinCheckInvoiceRecordDetailTab_New.USDPercent16
                        - modelFinCheckInvoiceRecordDetailTab_New.USDPercent15
                        - modelFinCheckInvoiceRecordDetailTab_New.USDPercent13
                        - modelFinCheckInvoiceRecordDetailTab_New.USDPercent9
                        - modelFinCheckInvoiceRecordDetailTab_New.USDPercent5;
                        //- modelFinCheckInvoiceRecordDetailTab_New.HKPercent17
                        //- modelFinCheckInvoiceRecordDetailTab_New.HKPercent16
                        //- modelFinCheckInvoiceRecordDetailTab_New.HKPercent15
                        //- modelFinCheckInvoiceRecordDetailTab_New.HKPercent13
                        //- modelFinCheckInvoiceRecordDetailTab_New.HKPercent9
                        //- modelFinCheckInvoiceRecordDetailTab_New.HKPercent5;

                    modelFinCheckInvoiceRecordDetailTab_New.ShouPiaoJinE = -(
                        -modelFinCheckInvoiceRecordDetailTab_New.USDPercent17
          - modelFinCheckInvoiceRecordDetailTab_New.USDPercent16
          - modelFinCheckInvoiceRecordDetailTab_New.USDPercent15
          - modelFinCheckInvoiceRecordDetailTab_New.USDPercent13
          - modelFinCheckInvoiceRecordDetailTab_New.USDPercent9
          - modelFinCheckInvoiceRecordDetailTab_New.USDPercent5);
          //- modelFinCheckInvoiceRecordDetailTab_New.HKPercent17
          //- modelFinCheckInvoiceRecordDetailTab_New.HKPercent16
          //- modelFinCheckInvoiceRecordDetailTab_New.HKPercent15
          //- modelFinCheckInvoiceRecordDetailTab_New.HKPercent13
          //- modelFinCheckInvoiceRecordDetailTab_New.HKPercent9
          //- modelFinCheckInvoiceRecordDetailTab_New.HKPercent5);
                }
                else
                {
                    CurrentUGR.Cells["报关开票差额"].Value = Convert.ToDecimal(CurrentUGR.Cells["报关金额"].Value.ToString())
                     //- modelFinCheckInvoiceRecordDetailTab_New.USDPercent17
                     //- modelFinCheckInvoiceRecordDetailTab_New.USDPercent16
                     //- modelFinCheckInvoiceRecordDetailTab_New.USDPercent15
                     //- modelFinCheckInvoiceRecordDetailTab_New.USDPercent13
                     //- modelFinCheckInvoiceRecordDetailTab_New.USDPercent9
                     //- modelFinCheckInvoiceRecordDetailTab_New.USDPercent5
                     - modelFinCheckInvoiceRecordDetailTab_New.HKPercent17
                     - modelFinCheckInvoiceRecordDetailTab_New.HKPercent16
                     - modelFinCheckInvoiceRecordDetailTab_New.HKPercent15
                     - modelFinCheckInvoiceRecordDetailTab_New.HKPercent13
                     - modelFinCheckInvoiceRecordDetailTab_New.HKPercent9
                     - modelFinCheckInvoiceRecordDetailTab_New.HKPercent5;

                    modelFinCheckInvoiceRecordDetailTab_New.ShouPiaoJinE = -(
          //              -modelFinCheckInvoiceRecordDetailTab_New.USDPercent17
          //- modelFinCheckInvoiceRecordDetailTab_New.USDPercent16
          //- modelFinCheckInvoiceRecordDetailTab_New.USDPercent15
          //- modelFinCheckInvoiceRecordDetailTab_New.USDPercent13
          //- modelFinCheckInvoiceRecordDetailTab_New.USDPercent9
          //- modelFinCheckInvoiceRecordDetailTab_New.USDPercent5
          - modelFinCheckInvoiceRecordDetailTab_New.HKPercent17
          - modelFinCheckInvoiceRecordDetailTab_New.HKPercent16
          - modelFinCheckInvoiceRecordDetailTab_New.HKPercent15
          - modelFinCheckInvoiceRecordDetailTab_New.HKPercent13
          - modelFinCheckInvoiceRecordDetailTab_New.HKPercent9
          - modelFinCheckInvoiceRecordDetailTab_New.HKPercent5);
                }

                modelFinCheckInvoiceRecordDetailTab_New.DanJuDate = System.DateTime.Now.Date;
                if (modelFinCheckInvoiceRecordDetailTab_New.ShouPiaoJinE != modelFinCheckInvoiceRecordDetailTab_Old.ShouPiaoJinE)
                {
                    modelFinCheckInvoiceRecordDetailTab_New.BianHao = MakeRecvMoneyDetailDJNo().ToString();
                }

                m_bllFinCheckInvoiceRecordDetailTab_New.Update(modelFinCheckInvoiceRecordDetailTab_New);
                CurrentUGR.Cells["收票金额"].Value = modelFinCheckInvoiceRecordDetailTab_New.ShouPiaoJinE;
                CurrentUGR.Cells["单据日期"].Value = DateTime.Now.Date; 
                CurrentUGR.Cells["编号"].Value = modelFinCheckInvoiceRecordDetailTab_New.BianHao;
                UpdateShenPiaoDateOnHxdTab(modelFinCheckInvoiceRecordDetailTab_New.PaidanRegisterTabID);

                Frm_Main.Log2DB(this.Text, "修改", strDanWei);
                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private static void UpdateShenPiaoDateOnHxdTab(int nPaidanRegisterTabID)
        {
            DataSet ds2
                    = m_bllFinCheckInvoiceRecordDetailTab_New.GetFinInvoiceRecordTabInfo("PaidanRegisterTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID + "'");

            if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
            {
                Decimal dtJiaShuiTotal = 0;
                Decimal dtContractJinE = 0;
                DateTime datetimeTemp = DateTime.Parse("1900-01-01"); 
                
                for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
                {
                    int nID = (int)ds2.Tables[0].Rows[i]["FinCheckInvoiceRecordDetailTab_NewID"];
                    DGBY.Model.FinCheckInvoiceRecordDetailTab_New modelFIRTab_New = m_bllFinCheckInvoiceRecordDetailTab_New.GetModel(nID);
                    dtContractJinE = (Decimal)ds2.Tables[0].Rows[i]["报关金额"];
                    dtJiaShuiTotal += (Decimal)ds2.Tables[0].Rows[i]["开票USD设17"]
                        + (Decimal)ds2.Tables[0].Rows[i]["开票USD设16"]
                        + (Decimal)ds2.Tables[0].Rows[i]["开票USD设15"]
                        + (Decimal)ds2.Tables[0].Rows[i]["开票USD设13"]
                        + (Decimal)ds2.Tables[0].Rows[i]["开票USD设9"]
                        + (Decimal)ds2.Tables[0].Rows[i]["开票USD设5"];
                    DateTime dtTemp = (DateTime)ds2.Tables[0].Rows[i]["审票日期"];
                    if (dtTemp > datetimeTemp)
                        datetimeTemp = dtTemp;
                   
                }

                bool bIsKaiPiaoFinish = false;
                if (dtContractJinE - dtJiaShuiTotal > 0.1M
                    || dtJiaShuiTotal - dtContractJinE > 0.1M)
                {
                    bIsKaiPiaoFinish = false;
                }
                else
                {
                    bIsKaiPiaoFinish = true;
                }

                if (bIsKaiPiaoFinish)
                {
                    DataSet ds = m_bllHxdNoTab.GetList("PaidanRegisterTabID = '" + nPaidanRegisterTabID + "'");
                
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        int nHxdNoTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["HxdNoTabID"].ToString(), 10);
                        DGBY.Model.HxdNoTab modelHxdNoTab = m_bllHxdNoTab.GetModel(nHxdNoTabID);
                        modelHxdNoTab.CheckInvoiceDate = datetimeTemp;
                        m_bllHxdNoTab.Update(modelHxdNoTab);
                    }
                }
            }
        }
        private void ultraGrid1_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[e.Row.Index];

                if (m_bInsertFlag)
                {
                    ultraGrid1.Rows[e.Row.Index].Cells[2].Activation = Activation.ActivateOnly;
                    m_bInsertFlag = false;
                    if (!AddGoodsDetail(e.Row.Index))
                        e.Cancel = true;



                }
                else//修改
                {

                    ModifyGoodsDetail(e.Row.Index);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void ultraGrid1_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e)
        {
            e.DisplayPromptMsg = false;
            if (!m_bInsertFlag)
            {
                int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(ultraGrid1.ActiveRow.Cells[0].Value.ToString(), 10);
                DGBY.Model.FinCheckInvoiceRecordDetailTab_New modelFinRecvMoneyDetailTab_NewOld = m_bllFinCheckInvoiceRecordDetailTab_New.GetModel(nSelectFinRecvMoneyDetailTab_NewID);

                if (modelFinRecvMoneyDetailTab_NewOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    try
                    {

                        m_bllFinCheckInvoiceRecordDetailTab_New.Delete(nSelectFinRecvMoneyDetailTab_NewID);
                        Frm_Main.Log2DB(this.Text, "删除", ultraGrid1.ActiveRow.Cells["合同号"].Text);


                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        e.Cancel = true;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                MessageBox.Show("插入未完成，不能删除记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }



        private void GetTaxPointCount(int nIndex)
        {
            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

            string strContractNo = CurrentUGR.Cells["合同号"].Value.ToString();
            DataSet ds = m_bllBGCKHTTab.CountAllCKHTMXForBackTax(strContractNo);
            StringBuilder sb = new StringBuilder();
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 1)
            {
                //sb.Append("\n");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    sb.Append("退税点:");
                    sb.Append(ds.Tables[0].Rows[i]["退税点"].ToString());
                    sb.Append("    金额:");
                    sb.Append(ds.Tables[0].Rows[i]["金额"].ToString());
                    sb.Append("\n");
                }

                this.richTextBoxShowBackTax.Text = sb.ToString();
                //Frm_ShowInfoDialog frm_ShowInfoDialog = new Frm_ShowInfoDialog(sb.ToString());
                //frm_ShowInfoDialog.StartPosition = FormStartPosition.CenterScreen;
                //frm_ShowInfoDialog.ShowDialog();
            }
            else
            {
                this.richTextBoxShowBackTax.Text = "";
            }
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            m_nDelIndex = ultraGrid1.ActiveRow.Index;
            if (m_bSumStart)
            {
                if (this.ultraGrid1.ActiveCell.Appearance.BackColor.Name == "0")
                {
                    this.ultraGrid1.ActiveCell.Appearance.BackColor = Color.Aqua;
                }
                else
                {
                    this.ultraGrid1.ActiveCell.Appearance.BackColor = Color.FromName("0");
                }
            }
            GetTaxPointCount(m_nDelIndex);
        }

        private void ultraGrid1_AfterRowCancelUpdate(object sender, RowEventArgs e)
        {
            m_bInsertFlag = false;
        }

        private void ultraGrid1_BeforeCellCancelUpdate(object sender, CancelableCellEventArgs e)
        {
            m_bInsertFlag = false;
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            this.ultraGridPrintDocument1.Grid = this.ultraGrid1;
            this.ultraPrintPreviewDialog1.Document = this.ultraGridPrintDocument1;
            this.ultraPrintPreviewDialog1.ShowDialog(this);
        }
        private void toolStripMenuItemAdd_Click(object sender, EventArgs e)
        {
            //if (!m_bInsertFlag)
            //{
            //    UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
            //    //  row.Cells[2] = DateTime.Parse("1900-01-01");
            //}
        }

        private void toolStripMenuItemDel_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确实要删除该记录么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            int nIndex = m_nDelIndex;
            if (m_nDelIndex >= 0)
            {
                if (ultraGrid1.ActiveRow != null)
                {
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];
                    int nPaidanRegisterTabID = Convert.ToInt32(CurrentUGR.Cells["派单ID"].Value.ToString(), 10);

                    DataSet ds = m_bllFinCheckInvoiceRecordDetailTab_New.GetFinInvoiceRecordTabInfo("PaidanRegisterTab.PaidanRegisterTabID = '" + nPaidanRegisterTabID.ToString() + "'");

                    if (ds.Tables[0].Rows.Count > 1)
                    {
                        ultraGrid1.ActiveRow.Delete(false);
                    }
                    else
                    {
                        MessageBox.Show("系统中该合同号只剩下一条记录，不能删除!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
            }
            else
            {
                MessageBox.Show("请选中要删除的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void SetFormula(Control control, string formula)
        {
            // To acces extender the CalcManager extender properties, call 
            // the first get the GetCalcSettings method.
            CalcSettings objCalcSettings = null;
            objCalcSettings = this.ultraCalcManager1.GetCalcSettings(control);

            // Set the formula. 
            objCalcSettings.Formula = formula;
        }

        private void textBoxHuiLvFormula_TextChanged(object sender, EventArgs e)
        {
            SetFormula(this.textBoxHuiLvValue, this.textBoxHuiLvFormula.Text);
        }

        private void textBoxHuiLvValue_TextChanged(object sender, EventArgs e)
        {
            UltraGridRow CurrentUGR = ultraGrid1.Rows[m_nSelectRowIndex];
            CurrentUGR.Cells["USD金额"].Value = Convert.ToDecimal(this.textBoxHuiLvValue.Text);
            ModifyGoodsDetail(m_nSelectRowIndex);
        }

        private void ultraGrid1_AfterCellUpdate(object sender, CellEventArgs e)
        {
            if (e.Cell.Column.Key == "收票金额公式")
            {
                m_nSelectRowIndex = e.Cell.Row.Index;
                UltraGridRow CurrentUGR = ultraGrid1.Rows[m_nSelectRowIndex];
                if (e.Cell.Value.ToString() != "")
                {
                    int nFinCheckInvoiceRecordDetailTab_NewID = (int)CurrentUGR.Cells["FinCheckInvoiceRecordDetailTab_NewID"].Value;
                    DGBY.Model.FinCheckInvoiceRecordDetailTab_New modelFinCheckInvoiceRecordDetailTab_New
                        = m_bllFinCheckInvoiceRecordDetailTab_New.GetModel(nFinCheckInvoiceRecordDetailTab_NewID);

                    int nPaiDanID = (int)CurrentUGR.Cells["派单ID"].Value;
                    string strBizhi = CurrentUGR.Cells["币制"].Value.ToString();
                    Decimal dtTemp = this.ultraCalcManager1.Calculate(e.Cell.Value.ToString()).ToDecimal();

                    Decimal dtSetPercent = 0;
                    DataSet dsInfo = m_bllFactoryGoodsTab.GetGoodsBandInfo("PaidanRegisterTab.PaidanRegisterTabID = '"
            + nPaiDanID.ToString() + "'");
                    if (dsInfo.Tables.Count > 0 && dsInfo.Tables[0].Rows.Count > 0)
                    {
                        dtSetPercent = Convert.ToDecimal(dsInfo.Tables[0].Rows[0]["设百分比"].ToString());
                    }

                    if (strBizhi == "港币" && dtSetPercent == 0.17M)
                    {
                        CurrentUGR.Cells["开票HK设17"].Value = dtTemp;
                    }
                    else if (strBizhi == "港币" && dtSetPercent == 0.16M)
                    {
                        CurrentUGR.Cells["开票HK设16"].Value = dtTemp;
                    }
                    else if (strBizhi == "港币" && dtSetPercent == 0.15M)
                    {
                        CurrentUGR.Cells["开票HK设15"].Value = dtTemp;
                    }
                    else if (strBizhi == "港币" && dtSetPercent == 0.13M)
                    {
                        CurrentUGR.Cells["开票HK设13"].Value = dtTemp;
                    }
                    else if (strBizhi == "港币" && dtSetPercent == 0.09M)
                    {
                        CurrentUGR.Cells["开票HK设9"].Value = dtTemp;
                    }
                    else if (strBizhi == "港币" && dtSetPercent == 0.05M)
                    {
                        CurrentUGR.Cells["开票HK设5"].Value = dtTemp;
                    }
                    else if (strBizhi == "美元" && dtSetPercent == 0.17M)
                    {
                        CurrentUGR.Cells["开票USD设17"].Value = dtTemp;
                    }
                    else if (strBizhi == "美元" && dtSetPercent == 0.16M)
                    {
                        CurrentUGR.Cells["开票USD设16"].Value = dtTemp;
                    }
                    else if (strBizhi == "美元" && dtSetPercent == 0.15M)
                    {
                        CurrentUGR.Cells["开票USD设15"].Value = dtTemp;
                    }
                    else if (strBizhi == "美元" && dtSetPercent == 0.13M)
                    {
                        CurrentUGR.Cells["开票USD设13"].Value = dtTemp;
                    }
                    else if (strBizhi == "美元" && dtSetPercent == 0.09M)
                    {
                        CurrentUGR.Cells["开票USD设9"].Value = dtTemp;
                    }
                    else if (strBizhi == "美元" && dtSetPercent == 0.05M)
                    {
                        CurrentUGR.Cells["开票USD设5"].Value = dtTemp;
                    }

                }
                    //CurrentUGR.Cells["收票金额"].Value =  
            }
            else if (e.Cell.Column.Key == "收票金额")
            {
                //UltraGridRow CurrentUGR = ultraGrid1.Rows[e.Cell.Row.Index];

             
            }
        }

//        private void ProcessSetPercent()
//        {
//            m_SQLStringList.Clear();
//            m_arrayList.Clear();
//            DataSet ds = m_bllFinCheckInvoiceRecordDetailTab_New.GetFinInvoiceRecordTabInfo(@"
//            usdpercent17 + usdpercent16 + usdpercent15 + usdpercent13 + usdpercent9 + usdpercent5
//+ hkpercent17 + hkpercent16 + hkpercent15 + hkpercent13 + hkpercent9 + hkpercent5 = 0");

//            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
//            {
//                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
//                {
//                    int nFinCheckInvoiceRecordDetailTab_NewID = (int)ds.Tables[0].Rows[i]["FinCheckInvoiceRecordDetailTab_NewID"];
//                    DGBY.Model.FinCheckInvoiceRecordDetailTab_New modelFinCheckInvoiceRecordDetailTab_New
//                        = m_bllFinCheckInvoiceRecordDetailTab_New.GetModel(nFinCheckInvoiceRecordDetailTab_NewID);

//                    int nPaiDanID = (int)ds.Tables[0].Rows[i]["派单ID"];
//                    string strBizhi = ds.Tables[0].Rows[i]["币制"].ToString();
//                    Decimal dtTemp = (Decimal)ds.Tables[0].Rows[i]["报关金额"];

//                    Decimal dtSetPercent = 0;
//                    DataSet dsInfo = m_bllFactoryGoodsTab.GetGoodsBandInfo("PaidanRegisterTab.PaidanRegisterTabID = '"
//            + nPaiDanID.ToString() + "'");
//                    if (dsInfo.Tables.Count > 0 && dsInfo.Tables[0].Rows.Count > 0)
//                    {
//                        dtSetPercent = Convert.ToDecimal(dsInfo.Tables[0].Rows[0]["设百分比"].ToString());
//                    }

//                    if (strBizhi == "港币" && dtSetPercent == 0.17M)
//                    {
//                        modelFinCheckInvoiceRecordDetailTab_New.HKPercent17 = dtTemp;
//                    }
//                    else if (strBizhi == "港币" && dtSetPercent == 0.16M)
//                    {
//                        modelFinCheckInvoiceRecordDetailTab_New.HKPercent16 = dtTemp;
//                    }
//                    else if (strBizhi == "港币" && dtSetPercent == 0.15M)
//                    {
//                        modelFinCheckInvoiceRecordDetailTab_New.HKPercent15 = dtTemp;
//                    }
//                    else if (strBizhi == "港币" && dtSetPercent == 0.13M)
//                    {
//                        modelFinCheckInvoiceRecordDetailTab_New.HKPercent13 = dtTemp;
//                    }
//                    else if (strBizhi == "港币" && dtSetPercent == 0.09M)
//                    {
//                        modelFinCheckInvoiceRecordDetailTab_New.HKPercent9 = dtTemp;
//                    }
//                    else if (strBizhi == "港币" && dtSetPercent == 0.05M)
//                    {
//                        modelFinCheckInvoiceRecordDetailTab_New.HKPercent5 = dtTemp;
//                    }
//                    else if (strBizhi == "美元" && dtSetPercent == 0.17M)
//                    {
//                        modelFinCheckInvoiceRecordDetailTab_New.USDPercent17 = dtTemp;
//                    }
//                    else if (strBizhi == "美元" && dtSetPercent == 0.16M)
//                    {
//                        modelFinCheckInvoiceRecordDetailTab_New.USDPercent16 = dtTemp;
//                    }
//                    else if (strBizhi == "美元" && dtSetPercent == 0.15M)
//                    {
//                        modelFinCheckInvoiceRecordDetailTab_New.USDPercent15 = dtTemp;
//                    }
//                    else if (strBizhi == "美元" && dtSetPercent == 0.13M)
//                    {
//                        modelFinCheckInvoiceRecordDetailTab_New.USDPercent13 = dtTemp;
//                    }
//                    else if (strBizhi == "美元" && dtSetPercent == 0.09M)
//                    {
//                        modelFinCheckInvoiceRecordDetailTab_New.USDPercent9 = dtTemp;
//                    }
//                    else if (strBizhi == "美元" && dtSetPercent == 0.05M)
//                    {
//                        modelFinCheckInvoiceRecordDetailTab_New.USDPercent5 = dtTemp;
//                    }

//                    m_bllFinCheckInvoiceRecordDetailTab_New.TransUpdate(modelFinCheckInvoiceRecordDetailTab_New
//                        , m_SQLStringList, m_arrayList, i);

//                    ProcessFinCheckInvoiceRecordDetailAuxiliaryTab_New(0, dtTemp, modelFinCheckInvoiceRecordDetailTab_New, i);

//                }
//                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
               
//            }


//        }

        //public static void onModifyHxdSheet(object sender, RetrieveInfoGeneralEventArgs e)
        //{
        //    UpdateShenPiaoDateOnHxdTab(e.m_nPaidanID);
        //}

        private void Frm_FinInvoiceCheckRec_Load(object sender, EventArgs e)
        {
            ClassUltraGridCommonSet.InitUltraWinGridSetting(this.ultraGrid1);
          //  ProcessSetPercent();
          //  Frm_RetrieveInfoGeneral.onModify += onModifyHxdSheet;
        }

        private void buttonBacthMod_Click(object sender, EventArgs e)
        {
            m_nAppendSpaceNum = 0;
            m_SQLStringList.Clear();
            m_arrayList.Clear();
            Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
            if (ugr.Length <= 0)
            {
                MessageBox.Show("没有要批量修改的行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            for (int i = 0; i < ugr.Length; i++)
            {
                int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                DGBY.Model.FinCheckInvoiceRecordDetailTab_New modelFinCheckInvoiceRecordDetailTab_New
                    = m_bllFinCheckInvoiceRecordDetailTab_New.GetModel(nID);
                //Decimal dtOldUSDJinE = ComputeUSD(modelFinCheckInvoiceRecordDetailTab_New);
                modelFinCheckInvoiceRecordDetailTab_New.USDHuiLv = Convert.ToDecimal(this.textBoxUsdHuiLv.Text);
                modelFinCheckInvoiceRecordDetailTab_New.HKHuiLv = Convert.ToDecimal(this.textBoxHKHuiLv.Text);

                Dictionary<Decimal, Decimal> dicNewPrecent2JinE = ComputeUSD2(modelFinCheckInvoiceRecordDetailTab_New);
                ProcessFinCheckInvoiceRecordDetailAuxiliaryTab_New(dicNewPrecent2JinE, modelFinCheckInvoiceRecordDetailTab_New);
                m_bllFinCheckInvoiceRecordDetailTab_New.TransUpdate(modelFinCheckInvoiceRecordDetailTab_New
                    , m_SQLStringList, m_arrayList, i);
            }

            m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);

            MessageBox.Show("批量修改汇率成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        //private void buttonReflesh_Click(object sender, EventArgs e)
        //{
        //    ProcessSetPercent();
        //}

        private void buttonCheckJinE_Click(object sender, EventArgs e)
        {
            string strNotify = "";
            DataSet ds = m_bllFinCheckInvoiceRecordDetailTab_New.GetFinInvoiceRecordTabInfo("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string strContract = ds.Tables[0].Rows[i]["合同号"].ToString();

                    Decimal dtContractJinE = Convert.ToDecimal(ds.Tables[0].Rows[i]["报关金额"].ToString());
                    Decimal dtJiaShuiTotal = (Decimal)ds.Tables[0].Rows[i]["开票USD设17"]
                        + (Decimal)ds.Tables[0].Rows[i]["开票USD设16"]
                        + (Decimal)ds.Tables[0].Rows[i]["开票USD设15"]
                        + (Decimal)ds.Tables[0].Rows[i]["开票USD设13"]
                        + (Decimal)ds.Tables[0].Rows[i]["开票USD设9"]
                        + (Decimal)ds.Tables[0].Rows[i]["开票USD设5"]
                        + (Decimal)ds.Tables[0].Rows[i]["开票HK设17"]
                        + (Decimal)ds.Tables[0].Rows[i]["开票HK设16"]
                        + (Decimal)ds.Tables[0].Rows[i]["开票HK设15"]
                        + (Decimal)ds.Tables[0].Rows[i]["开票HK设13"]
                        + (Decimal)ds.Tables[0].Rows[i]["开票HK设9"]
                        + (Decimal)ds.Tables[0].Rows[i]["开票HK设5"];
                    Decimal dtDiff = dtContractJinE - dtJiaShuiTotal;
                    if (dtDiff > 0)
                    {
                        continue;
                    }

                    ds.Tables[0].Rows.RemoveAt(i);
                    i--;
                }

                ultraGrid1.DataSource = null;


                ultraGrid1.DataSource = ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;


            }

          //  Frm_Main.Notify("金额检查", "没有合同号合同金额与价税合计之差大于0.1元\n" + strNotify, 300, 200, 3000);


        }

        private void buttonSumStart_Click(object sender, EventArgs e)
        {
            m_bSumStart = true;
            Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
            for (int i = 0; i < ugr.Length; i++)
            {
                for (int j = 0; j < ugr[i].Cells.Count; j++)
                {
                    if (ugr[i].Cells[j].Appearance.BackColor == Color.Aqua)
                    {
                        ugr[i].Cells[j].Appearance.BackColor = Color.FromName("0");
                    }
                }
            }
        }

        private void buttonSumStop_Click(object sender, EventArgs e)
        {
            try
            {
                Decimal dtTotal = 0;
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
                for (int i = 0; i < ugr.Length; i++)
                {
                    for (int j = 0; j < ugr[i].Cells.Count; j++)
                    {
                        if (ugr[i].Cells[j].Appearance.BackColor == Color.Aqua)
                        {
                            Decimal dtTemp = Convert.ToDecimal(ugr[i].Cells[j].Value.ToString());
                            dtTotal += dtTemp;
                            //ugr[i].Cells[j].Appearance.BackColor = Color.FromName("0");
                        }
                    }
                }

                this.textBoxTotal.Text = dtTotal.ToString();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }
        private void ultraGrid1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                UltraGrid grid = (UltraGrid)sender;
                UIElement element = grid.DisplayLayout.UIElement.LastElementEntered;

                Infragistics.Win.UltraWinGrid.ColumnHeader columnHeader = element.GetContext(typeof(Infragistics.Win.UltraWinGrid.ColumnHeader)) as Infragistics.Win.UltraWinGrid.ColumnHeader;
                if (columnHeader != null)
                {
                    string str = columnHeader.Caption;
                    if (str != "合同金额" && str != "报关金额")
                        return;
                    Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
                    if (ugr.Length <= 0)
                    {
                        return;
                    }
                    Decimal dtTemp = 0;
                    for (int i = 0; i < ugr.Length; i++)
                    {
                        string strTemp = ugr[i].Cells[str].Value.ToString();
                        if (strTemp != "")
                            dtTemp += Convert.ToDecimal(ugr[i].Cells[str].Value.ToString());
                    }
                    this.textBoxTotal.Text = dtTemp.ToString();


                    //if(bool f=age is int;


                    // Set the grid's tooltip here
                    // Debug.WriteLine("ToolTip");
                }
                else
                {
                    // Set the grid's tooltip to null. 
                    //  Debug.WriteLine("null");
                }  
            }
            catch (System.Exception ex)
            {
                return;
            }
           
        }

        private void 复制增加一行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (!m_bInsertFlag)
                {
                    UltraGridRow CurrentUGRFrom;
                    CurrentUGRFrom = ultraGrid1.Rows[m_nDelIndex];
                    int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(CurrentUGRFrom.Cells[0].Value.ToString(), 10);
                    m_modelFinCheckInvoiceRecordDetailTab_New 
                        = m_bllFinCheckInvoiceRecordDetailTab_New.GetModel(nSelectFinRecvMoneyDetailTab_NewID);


                    m_bCopyInsertFlag = true;
                    m_strFilter = CurrentUGRFrom.Cells["合同号"].Text;
                    UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();


                    //  row.Cells[2] = DateTime.Parse("1900-01-01");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("没有选择记录，无法复制！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonShowTax_Click(object sender, EventArgs e)
        {
            ListCountAllCKHTMXForBackTax();
        }

        private void ListCountAllCKHTMXForBackTax()
        {
            DataSet ds = m_bllBGCKHTTab.ListCountAllCKHTMXForBackTax("");

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
                for (int i = 0; i < ugr.Length; i++)
                {
                    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                    {
                        string strContract = ds.Tables[0].Rows[j]["合同号"].ToString();
                        if (ugr[i].Cells["合同号"].Value.ToString() == strContract)
                        {
                            ugr[i].Appearance.BackColor = Color.Gray;
                            break;
                        }
                    }


                    //ugr[i].Cells[3].Appearance.BackColor
                }

            }
        }

        private void buttonReflesh_Click(object sender, EventArgs e)
        {

        }
        //private void buttonReflesh_Click(object sender, EventArgs e)
        //{

        //}

    }
}
